package com.nanyang.tms.controller.ticket;

import javax.validation.Valid;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.nanyang.common.core.annotation.BindParam;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.tms.api.dto.request.ticket.TicketAddRequest;
import com.nanyang.tms.api.dto.request.ticket.TicketImportRequest;
import com.nanyang.tms.api.dto.request.ticket.TicketQueryRequest;
import com.nanyang.tms.api.dto.request.ticket.TicketRecordQueryRequest;
import com.nanyang.tms.api.dto.request.ticket.TicketUpdateRequest;
import com.nanyang.tms.api.dto.response.ticket.TicketPageResponse;
import com.nanyang.tms.api.dto.response.ticket.TicketRecordResponse;
import com.nanyang.tms.api.dto.response.ticket.TicketResponse;
import com.nanyang.tms.api.service.ticket.TicketRpcService;

@RestController
@RequestMapping("/ticket")
public class TicketController {

    @Reference(version = "1.0.0", timeout = 10000, check = false)
    private TicketRpcService ticketRpcService;

    /**
     * <p>listTicket</p>
     * <p>Description:查询单号分类列表</p>
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResult<PageBean<TicketPageResponse>> listTicket(@RequestBody TicketQueryRequest param) {
        ApiResult<PageBean<TicketPageResponse>> apiResult = ticketRpcService.listTicketType(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * <p>listTicket</p>
     * <p>Description:查询根据编码</p>
     *
     * @param ticketCode
     * @return
     */
    @RequestMapping(value = "/selectByCode/{ticketCode}", method = RequestMethod.GET)
    public ApiResult<TicketResponse> selectByCode(@PathVariable("ticketCode") String ticketCode) {
        ApiResult<TicketResponse> apiResult = ticketRpcService.selectByCode(ticketCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 添加
     * @param param
     * @return
     */

    @BindParam
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ApiResult<Boolean> saveTicket(@BindParam @RequestBody @Valid TicketAddRequest param) {
        ApiResult<Boolean> apiResult = ticketRpcService.saveTicketType(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 修改
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResult<Boolean> updateTicket(  @BindParam @RequestBody @Valid TicketUpdateRequest param) {
        ApiResult<Boolean> apiResult = ticketRpcService.updateTicketType(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 删除
     * @param ticketCode
     * @return
     */
    @BindParam
    @RequestMapping(value = "/del/{ticketCode}", method = RequestMethod.GET)
    public ApiResult<Boolean> delTicket(@PathVariable("ticketCode") String ticketCode,@Value("") @BindParam String pin) {
        ApiResult<Boolean> apiResult = ticketRpcService.delTicketType(ticketCode,pin);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 查看单号
     * @param param
     * @return
     */
    @RequestMapping(value = "/listTicketRecord", method = RequestMethod.POST)
    public  ApiResult<PageBean<TicketRecordResponse>> listTicketRecord(@RequestBody TicketRecordQueryRequest param) {
        ApiResult<PageBean<TicketRecordResponse>> apiResult = ticketRpcService.listTicketRecord(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 导入
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public ApiResult<Boolean> updateTicket(  @BindParam @RequestBody @Valid TicketImportRequest param) {
        ApiResult<Boolean> apiResult = ticketRpcService.importTicketNo(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
}
